<?php

class EActiveDataProviderEx extends CActiveDataProvider {

    private $_cache_duration;
    private $_cache_dependency;

    public function getCache() {
        return array($this->_cache_duration, $this->_cache_dependency);
    }

    public function setCache($value) {
        if (is_array($value) && count($value)) {
            $this->_cache_duration = $value[0];
            if (count($value) > 1) {
                $this->_cache_dependency = $value[1];
            } else {
                $this->_cache_dependency = null;
            }
        } else {
            $this->_cache_duration = null;
            $this->_cache_dependency = null;
        }
    }

    protected function fetchData() {
        if(!is_null($this->_cache_duration))
            $this->model=$this->model->cache($this->_cache_duration, $this->_cache_dependency,2);
        return parent::fetchData();
    }

    protected function calculateTotalItemCount() {
        $baseCriteria=$this->model->getDbCriteria(false);
        if($baseCriteria!==null)
            $baseCriteria=clone $baseCriteria;
        if($this->getCriteria()->having || $this->getCriteria()->group)
            $count=$this->model->dbConnection->commandBuilder->createCountCommand($this->model->tableName(), $this->getCriteria())->queryScalar();
        else
            $count=$this->model->count($this->getCriteria());
        $this->model->setDbCriteria($baseCriteria);
        return $count;
    }
}

?>
